package com.bgs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bgs.common.JwtUtils;
import com.bgs.common.MD5;
import com.bgs.common.R;
import com.bgs.pojo.BeforeUser;
import com.bgs.service.BeforeUserService;
import com.bgs.service.TokenUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;

/**
 * @author Asia
 * @version 1.0
 * @description: TODO
 * @date 2021/8/16 10:43
 */
@Service
class TokenUserServiceImpl implements TokenUserService {
    @Autowired
    private BeforeUserService beforeUserService;
    @Override
    public R ThreeBeforeLogin(String beforeUserName ,String beforePassword) {

        QueryWrapper<BeforeUser> wrapper = new QueryWrapper<>();
        String pass = MD5.encrypt(beforePassword);
        wrapper.eq("before_user_name",beforeUserName);
        wrapper.eq("before_password",pass);
        BeforeUser one = beforeUserService.getOne(wrapper);
        if (one==null){
            return R.error().message("用户名不匹配");
        }
        if(!pass.equals(one.getBeforePassword())){
            return R.error().message("密码不正确");
        }
        //生成token
        String oneId=String.valueOf(one.getBeforeUserId());
        String jwtToken = JwtUtils.getJwtToken(oneId,one.getBeforeUserName());
        HashMap<String, Object> map = new HashMap<>();
        map.put("token",jwtToken);
        map.put("user",one);
        return R.ok().data("map",map);
    }
    @Override
    public R ThreeLogout() {

        return R.ok();
    }
}
